<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/24 15:06
 * Remark:
 */

namespace App\Http\Controllers\Position;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Position\AddPositionPost;
use App\Http\Requests\Position\ListPositionGet;
use App\Http\Requests\Position\SelectPositionGet;
use App\Service\Position\PositionService;

class PositionController extends Controller
{
    private PositionService $_positionService;

    public function __construct()
    {
        $this->middleware([
            'positionLeg'
        ]);
        $this->_positionService = new PositionService();
    }

    /**
     * 创建职位
     * @param AddPositionPost $request
     */
    public function createPosition(AddPositionPost $request): \Illuminate\Http\JsonResponse
    {
        $this->_positionService->createPosition($request);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '新建职位成功',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }

    /**
     * 编辑职位
     * @param int $positionId
     * @return mixed
     */
    public function editPosition(int $positionId): \Illuminate\Http\JsonResponse
    {
        $editData = \request()->post();
        $editData['position_id'] = $positionId;
        $this->_positionService->editPosition($editData);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '编辑职位成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 根据部门ID获取职位下拉
     * @param SelectPositionGet $request
     * @return mixed
     */
    public function getPositionListByDepartmentId(SelectPositionGet $request): mixed
    {
        $pageSize = $request->get('page_size');
        $departmentId = $request->get('department_id');
        $pFilters = $request->get('filters');

        return ResponseFunc::returnJson(
            'ok',
            $this->_positionService->getPositionListByDepartmentId($departmentId,$pageSize,$pFilters),
            '获取职位列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 根据部门ID获取职位下拉
     * @param SelectPositionGet $request
     * @return mixed
     */
    public function getPositionSelectByDepartmentId(SelectPositionGet $request): mixed
    {
        $departmentId = $request->get('department_id');
        return ResponseFunc::returnJson(
            'ok',
            $this->_positionService->getPositionSelectByDepartmentId($departmentId),
            '获取职位下拉成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 根据公司Id获取职位列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPositionListByCompanyId(ListPositionGet $request): \Illuminate\Http\JsonResponse
    {
        $pFilters = $request->get('filters');
        $pageSize = $request->get('page_size');
        $filters = $pFilters ? json_decode($pFilters,true) : [];
        $companyId = TokenFuc::getCurrentStaffInfo()['company_id'] ?? 0;
        return ResponseFunc::returnJson(
            'ok',
            $this->_positionService->getPositionListByCompanyId($companyId,$filters,$pageSize),
            '编辑公司成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 批量删除职位
     * @return \Illuminate\Http\JsonResponse
     */
    public function bulkDeletePosition(): \Illuminate\Http\JsonResponse
    {
        $positionIds = \request()->post('position_ids');
        $this->_positionService->bulkDeletePosition($positionIds);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '批量删除职位成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 删除职位
     * @param int $positionId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deletePosition(int $positionId): \Illuminate\Http\JsonResponse
    {
        $this->_positionService->editPosition([
            'position_id' => $positionId,
            'status' => BaseConsts::STATUS_DELETE
        ]);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除职位成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 获取职位详情
     * @param int $positionId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPositionDetail(int $positionId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_positionService->getPositionDetailByPositionId($positionId),
            '获取职位详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }


//    /**
//     * 启用职位
//     * @param int $positionId
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function startPosition(int $positionId): \Illuminate\Http\JsonResponse
//    {
//        if(request()->post('menu_ids')){
//            $positionData = request()->all();
//            $positionData['position_id'] = $positionId;
//            $positionData['position_status'] = BaseConsts::STATUS_START;
//            $positionData['position_menu'] =  implode(',',request()->post('menu_ids'));
//            return ResponseFunc::returnJson(
//                'ok',
//                $this->_positionService->startPosition($positionData),
//                '启用职位成功',
//                ResponseStatusConsts::OK_200_RESPONSE_STATUS
//            );
//        }
//        return ResponseFunc::returnJson(
//            'error',
//            null,
//            '菜单不存在或已删除',
//            ResponseStatusConsts::OK_200_RESPONSE_STATUS
//        );
//
//    }


}